import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _typeof from "@babel/runtime/helpers/esm/typeof";
var _excluded = ["title", "content", "pageHeaderRender", "header", "prefixedClassName", "extraContent", "style", "prefixCls", "breadcrumbRender"],
    _excluded2 = ["children", "loading", "className", "style", "footer", "affixProps", "ghost", "fixedHeader", "breadcrumbRender"];
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
import { Affix, Breadcrumb, ConfigProvider, PageHeader, Tabs } from 'antd-v4';
import classNames from 'classnames';
import React, { useContext, useMemo } from 'react';
import RouteContext from '../../RouteContext';
import FooterToolbar from '../FooterToolbar';
import GridContent from '../GridContent';
import PageLoading from '../PageLoading';
import WaterMark from '../WaterMark';
import './index.less';

function genLoading(spinProps) {
  if (_typeof(spinProps) === 'object') {
    return spinProps;
  }

  return {
    spinning: spinProps
  };
}
/**
 * Render Footer tabList In order to be compatible with the old version of the PageHeader basically
 * all the functions are implemented.
 */


var renderFooter = function renderFooter(_ref) {
  var tabList = _ref.tabList,
      tabActiveKey = _ref.tabActiveKey,
      onTabChange = _ref.onTabChange,
      tabBarExtraContent = _ref.tabBarExtraContent,
      tabProps = _ref.tabProps,
      prefixedClassName = _ref.prefixedClassName;

  if (Array.isArray(tabList) || tabBarExtraContent) {
    return _jsx(Tabs, _objectSpread(_objectSpread({
      className: "".concat(prefixedClassName, "-tabs"),
      activeKey: tabActiveKey,
      onChange: function onChange(key) {
        if (onTabChange) {
          onTabChange(key);
        }
      },
      tabBarExtraContent: tabBarExtraContent
    }, tabProps), {}, {
      children: tabList === null || tabList === void 0 ? void 0 : tabList.map(function (item, index) {
        return (
          /*#__PURE__*/
          // eslint-disable-next-line react/no-array-index-key
          _createElement(Tabs.TabPane, _objectSpread(_objectSpread({}, item), {}, {
            tab: item.tab,
            key: item.key || index
          }))
        );
      })
    }));
  }

  return null;
};

var renderPageHeader = function renderPageHeader(content, extraContent, prefixedClassName) {
  if (!content && !extraContent) {
    return null;
  }

  return _jsx("div", {
    className: "".concat(prefixedClassName, "-detail"),
    children: _jsx("div", {
      className: "".concat(prefixedClassName, "-main"),
      children: _jsxs("div", {
        className: "".concat(prefixedClassName, "-row"),
        children: [content && _jsx("div", {
          className: "".concat(prefixedClassName, "-content"),
          children: content
        }), extraContent && _jsx("div", {
          className: "".concat(prefixedClassName, "-extraContent"),
          children: extraContent
        })]
      })
    })
  });
};
/**
 * 配置与面包屑相同，只是增加了自动根据路由计算面包屑的功能。此功能必须要在 ProLayout 中使用。
 *
 * @param props
 * @returns
 */


var ProBreadcrumb = function ProBreadcrumb(props) {
  var value = useContext(RouteContext);
  return _jsx("div", {
    style: {
      height: '100%',
      display: 'flex',
      alignItems: 'center'
    },
    children: _jsx(Breadcrumb, _objectSpread(_objectSpread(_objectSpread({}, value === null || value === void 0 ? void 0 : value.breadcrumb), value === null || value === void 0 ? void 0 : value.breadcrumbProps), props))
  });
};

var ProPageHeader = function ProPageHeader(props) {
  var _breadcrumb$routes;

  var value = useContext(RouteContext);

  var title = props.title,
      content = props.content,
      pageHeaderRender = props.pageHeaderRender,
      header = props.header,
      prefixedClassName = props.prefixedClassName,
      extraContent = props.extraContent,
      style = props.style,
      prefixCls = props.prefixCls,
      breadcrumbRender = props.breadcrumbRender,
      restProps = _objectWithoutProperties(props, _excluded);

  var getBreadcrumbRender = useMemo(function () {
    if (!breadcrumbRender) {
      return undefined;
    }

    return breadcrumbRender;
  }, [breadcrumbRender]);

  if (pageHeaderRender === false) {
    return null;
  }

  if (pageHeaderRender) {
    return _jsxs(_Fragment, {
      children: [" ", pageHeaderRender(_objectSpread(_objectSpread({}, props), value))]
    });
  }

  var pageHeaderTitle = title;

  if (!title && title !== false) {
    pageHeaderTitle = value.title;
  }

  var pageHeaderProps = _objectSpread(_objectSpread(_objectSpread({}, value), {}, {
    title: pageHeaderTitle
  }, restProps), {}, {
    footer: renderFooter(_objectSpread(_objectSpread({}, restProps), {}, {
      breadcrumbRender: breadcrumbRender,
      prefixedClassName: prefixedClassName
    }))
  }, header);

  var breadcrumb = pageHeaderProps.breadcrumb;
  var noHasBreadCrumb = (!breadcrumb || !(breadcrumb === null || breadcrumb === void 0 ? void 0 : breadcrumb.itemRender) && !(breadcrumb === null || breadcrumb === void 0 ? void 0 : (_breadcrumb$routes = breadcrumb.routes) === null || _breadcrumb$routes === void 0 ? void 0 : _breadcrumb$routes.length)) && !breadcrumbRender;

  if (['title', 'subTitle', 'extra', 'tags', 'footer', 'avatar', 'backIcon'].every(function (item) {
    return !pageHeaderProps[item];
  }) && noHasBreadCrumb && !content && !extraContent) {
    return null;
  }

  return _jsx("div", {
    className: "".concat(prefixedClassName, "-warp"),
    children: _jsx(PageHeader, _objectSpread(_objectSpread({}, pageHeaderProps), {}, {
      breadcrumb: breadcrumbRender === false ? undefined : _objectSpread(_objectSpread({}, pageHeaderProps.breadcrumb), value.breadcrumbProps),
      breadcrumbRender: getBreadcrumbRender,
      prefixCls: prefixCls,
      children: (header === null || header === void 0 ? void 0 : header.children) || renderPageHeader(content, extraContent, prefixedClassName)
    }))
  });
};

var PageContainer = function PageContainer(props) {
  var _classNames, _restProps$header2;

  var children = props.children,
      _props$loading = props.loading,
      loading = _props$loading === void 0 ? false : _props$loading,
      className = props.className,
      style = props.style,
      footer = props.footer,
      affixProps = props.affixProps,
      ghost = props.ghost,
      fixedHeader = props.fixedHeader,
      breadcrumbRender = props.breadcrumbRender,
      restProps = _objectWithoutProperties(props, _excluded2);

  var value = useContext(RouteContext);

  var _useContext = useContext(ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = props.prefixCls || getPrefixCls('pro');
  var prefixedClassName = "".concat(prefixCls, "-page-container");
  var containerClassName = classNames(prefixedClassName, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-page-container-ghost"), ghost), _defineProperty(_classNames, "".concat(prefixCls, "-page-container-with-footer"), footer), _classNames));
  var content = useMemo(function () {
    return children ? _jsxs(_Fragment, {
      children: [_jsx("div", {
        className: "".concat(prefixedClassName, "-children-content"),
        children: children
      }), value.hasFooterToolbar && _jsx("div", {
        style: {
          height: 48,
          marginTop: 24
        }
      })]
    }) : null;
  }, [children, prefixedClassName, value.hasFooterToolbar]);
  var memoBreadcrumbRender = useMemo(function () {
    var _restProps$header;

    if (breadcrumbRender == false) return false;
    return breadcrumbRender || (restProps === null || restProps === void 0 ? void 0 : (_restProps$header = restProps.header) === null || _restProps$header === void 0 ? void 0 : _restProps$header.breadcrumbRender);
  }, [breadcrumbRender, restProps === null || restProps === void 0 ? void 0 : (_restProps$header2 = restProps.header) === null || _restProps$header2 === void 0 ? void 0 : _restProps$header2.breadcrumbRender]);

  var pageHeaderDom = _jsx(ProPageHeader, _objectSpread(_objectSpread({}, restProps), {}, {
    breadcrumbRender: memoBreadcrumbRender,
    ghost: ghost,
    prefixCls: undefined,
    prefixedClassName: prefixedClassName
  }));

  var loadingDom = useMemo(function () {
    // 当loading时一个合法的ReactNode时，说明用户使用了自定义loading,直接返回改自定义loading
    if ( /*#__PURE__*/React.isValidElement(loading)) {
      return loading;
    } // 当传递过来的是布尔值，并且为false时，说明不需要显示loading,返回null


    if (typeof loading === 'boolean' && !loading) {
      return null;
    } // 如非上述两种情况，那么要么用户传了一个true,要么用户传了loading配置，使用genLoading生成loading配置后返回PageLoading


    var spinProps = genLoading(loading);
    return _jsx(PageLoading, _objectSpread({}, spinProps));
  }, [loading]);
  var renderContentDom = useMemo(function () {
    // 只要loadingDom非空我们就渲染loadingDom,否则渲染内容
    var dom = loadingDom || content;

    if (props.waterMarkProps || value.waterMarkProps) {
      var waterMarkProps = _objectSpread(_objectSpread({}, value.waterMarkProps), props.waterMarkProps);

      return _jsx(WaterMark, _objectSpread(_objectSpread({}, waterMarkProps), {}, {
        children: dom
      }));
    }

    return dom;
  }, [props.waterMarkProps, value.waterMarkProps, loadingDom, content]);
  return _jsxs("div", {
    style: style,
    className: containerClassName,
    children: [fixedHeader && pageHeaderDom ? // 在 hasHeader 且 fixedHeader 的情况下，才需要设置高度
    _jsx(Affix, _objectSpread(_objectSpread({
      offsetTop: value.hasHeader && value.fixedHeader ? value.headerHeight : 0
    }, affixProps), {}, {
      children: pageHeaderDom
    })) : pageHeaderDom, renderContentDom && _jsx(GridContent, {
      children: renderContentDom
    }), footer && _jsx(FooterToolbar, {
      prefixCls: prefixCls,
      children: footer
    })]
  });
};

export { ProPageHeader, ProBreadcrumb };
export default PageContainer;